$(document).ready(function() {
	//selectGroups();
	getWorkerType();
	$("select#worktype").change(function() {
		if ('diy' == $(this).val()) {
			workerTypeDiyShow();
		} else {
			workerTypeDiyHide();
		}
	});

	// 表单渲染
	$('#form1').submit(function() {
		var options = {
			url : ctp + "/workerController/workerSave.shtml",
			type : 'POST',
			success : function(result) {
				if (result == '1') {
					jalert9("保存成功！");
					setTimeout(function() {
						// parent.searchData();
						closeW();
					}, WARN_TIME_OUT);
				} else if (result == '0') {
					jalert("保存失败,请核实服务是否启动！");
				} else if (result == '2') {
					jalert("工号已存在，请更换！");
				} else if (result == '3') {
					jalert("手机号已存在，请更换！");
				} else {
					jalert("保存失败，请核实闸机系统服务是否启动以及工人照片是否是人脸！");
				}
			}
		};
		$('#form1').ajaxSubmit(options);
		return false;
	});
});

function workerTypeDiyShow() {
	$('#workerTypeDiy').show();
	$('#worktype').hide();
}
function workerTypeDiyHide() {
	$('#workerTypeDiy').hide();
	$('#worktype').show();
}

function selectGroups() {
	$.ajax({
		url : ctp + "/groupController/selectGroups.shtml",
		data : {},
		type : "POST",
		cache : false,
		dataType : "json",
		success : function(data) {
			var list = data.list;
			var $str = '';
			$str += '<select class="smart-body_select" name="groupid" id="groupid">';
			$str += '<option value="">--请选择--</option>';
			for (var i = 0; i < list.length; i++) {
				var l = list[i];
				$str += '<option value="' + l.REC_ID + '">' + l.NAME + '</option>';
			}
			$str += '</select>';
			$('#group').append($str);
		}
	});
}

function getWorkerType() {
	$.ajax({
		url : ctp + "/sysdictController/getDictListByType.shtml",
		data : {
			dicType : 'WORKER_TP'
		},
		type : "POST",
		cache : false,
		dataType : "json",
		success : function(data) {
			var list = data.list;
			var $str = '';
			$str += '<option value="">--请选择--</option>';
			for (var i = 0; i < list.length; i++) {
				var l = list[i];
				$str += '<option value="' + l.rec_value + '">' + l.rec_value + '</option>';
			}
			$str += '<option value="diy">自定义</option>';
			$('#worktype').html($str);
		}
	});
}

function uploadFile() {
	// alert(document.getElementById("contract").files[0].name);
	// alert(document.getElementById("contract").files[1].name);
	// alert(document.getElementById("contract").files[2].name);
	var files = document.getElementById("contract").files;
	var fileStr = "";
	for (var i = 0; i < files.length; i++) {
		fileStr += files[i].name + ',';
	}
	$('#file-name').html(fileStr);
}

function isPhoto(fileName) {
	var extStart = fileName.lastIndexOf('.');
	var ext = fileName.substring(extStart, fileName.length).toUpperCase();
	if (ext != '.BMP' && ext != '.PNG' && ext != '.GIF' && ext != '.JPG' && ext != '.JPEG') {
		jalert('抱歉，文件仅 png,gif,jpeg,jpg图片格式！');
		return false;
	} else {
		return true;
	}
}

// ajax 实现文件上传
function ajaxFileUpload() {
	$.ajaxFileUpload({
		url : ctp + "/workerController/photoUpload.shtml",
		secureuri : false,
		data : {
			p : new Date()
		},
		fileElementId : "photo",
		dataType : "text",
		success : function(data) {
			$('#LAY_demo_upload').attr("src", ctp + "/" + data);// 回显图片
			$('#photoPath').val(data);// 设置路径
		},
		error : function(data) {
			// 上传失败
		}
	});

}

function workerAdd() {
	setTagAndValue("form1","areaLName",$('#areaCode').val());
	setTagAndValue("form1","areaCode",$('#areaCode').attr("data-code"));
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	var worktype=$("#worktype").val();
	var workerTypeDiy=$("#workerTypeDiy").val();
	if(worktype=='diy'){
		if(workerTypeDiy.length==0){
			jalert('工种不能为空！');
			return;
		}
	}
	$('#form1').submit();
}

// 当前弹窗索引
var _index = parent.layui.layer.getFrameIndex(window.name);
function closeW() {
	// parent.layui.layer.close(_index);
	// location.href = ctp + "/workerController/worker.shtml";
	history.back();
}
